package fr.Castorflex.SpamYourFriend;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.telephony.SmsManager;

public class MyThread implements Runnable
{

	String[]	contact;
	String		message;
	int			nbPosts;
	Handler		progressHandler;
	Context		context;

	public MyThread(String[] contact, String message, int nbPosts,
			Handler progressHandler, Context c)
	{
		this.context = c;
		this.contact = contact;
		this.message = message;
		this.nbPosts = nbPosts;
		this.progressHandler = progressHandler;

	}

	@Override
	public void run()
	{
		SmsManager mgr = SmsManager.getDefault();

		PendingIntent sentIntent = PendingIntent.getBroadcast(context, 0,
				new Intent(Main.SENT), 0);
		String message2 = message +" ";
		String messToSend;
		int i = 0;
		try
		{
			while (i < nbPosts)
			{
				messToSend = (i % 2) == 0 ? message : message2;
				for (int j = 0; j < contact.length; ++j)
				{
					mgr.sendTextMessage(contact[j], null, messToSend, sentIntent,
							null);
				}
				++i;
			}

		} catch (Exception e)
		{
			// if something fails do something smart
		}

	}
}
